package chapter8.polymorphic.shape;
import static utility.Print.*;

import java.util.Random;

//: polymorphism/shape/Shape.java

class Shape {
	public void draw() {
	}

	public void erase() {
	}
} // /:~
// : polymorphism/shape/Circle.java

class Circle extends Shape {

	public void draw() {
		print("Circle.draw()");
	}

	public void erase() {
		print("Circle.erase()");
	}
} // /:~
// : polymorphism/shape/Square.java

class Square extends Shape {
	public void draw() {
		print("Square.draw()");
	}

	public void erase() {
		print("Square.erase()");
	}
} // /:~
// : polymorphism/shape/Triangle.java

class Triangle extends Shape {
	public void draw() {
		print("Triangle.draw()");
	}

	public void erase() {
		print("Triangle.erase()");
	}
} // /:~
// : polymorphism/shape/RandomShapeGenerator.java
// A "factory" that randomly creates shapes.

class RandomShapeGenerator {
	private Random rand = new Random(47);

	public Shape next() {
		switch (rand.nextInt(3)) {
		default:
		case 0:
			return new Circle();
		case 1:
			return new Square();
		case 2:
			return new Triangle();
		}
	}
} // /:~
// : polymorphism/Shapes.java
// Polymorphism in Java.

public class Shapes {
	private static RandomShapeGenerator gen = new RandomShapeGenerator();

	public static void main(String[] args) {
		Shape[] s = new Shape[9];
		// Fill up the array with shapes:
		for (int i = 0; i < s.length; i++)
			s[i] = gen.next();
		// Make polymorphic method calls:
		for (Shape shp : s)
			shp.draw();
	}
}
